package com.creditease.gateway.exception;

import com.creditease.gateway.utils.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author liuchao
 * @email liuchao332@163.com
 * @date 2020/3/3 14:36
 */
@RestControllerAdvice
public class ScheduleExceptionHandler {
	private Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * 处理自定义异常
	 */
	@ExceptionHandler(ScheduleException.class)
	public Response handleRRException(ScheduleException e){
		Response response = new Response();
		response.put("code", e.getCode());
		response.put("msg", e.getMessage());

		return response;
	}

	@ExceptionHandler(DuplicateKeyException.class)
	public Response handleDuplicateKeyException(DuplicateKeyException e){
		logger.error(e.getMessage(), e);
		return Response.error("数据库中已存在该记录");
	}

	@ExceptionHandler(Exception.class)
	public Response handleException(Exception e){
		logger.error(e.getMessage(), e);
		return Response.error();
	}
}
